ifeq ($(CFG_HI_EXPORT_FLAG),)
SDK_DIR := $(shell cd $(CURDIR)/../../../.. && /bin/pwd)
include $(SDK_DIR)/base.mak
endif

C_DEFINES+=-D_DRMENGINE_M_IX86 $(_DRM_C_DEFINES)


CFG_HI_TOOLCHAINS_NAME := $(CFG_HI_ARM_TOOLCHAINS_NAME)
CFG_HI_TOOLCHAINS_NAME_64 := $(CFG_HI_AARCH64_TOOLCHAINS_NAME)

DRM_CC := $(CFG_HI_TOOLCHAINS_NAME)-g++
DRM_AR := $(CFG_HI_TOOLCHAINS_NAME)-ar

DRM_CC_64 := $(CFG_HI_TOOLCHAINS_NAME_64)-g++
DRM_AR_64 := $(CFG_HI_TOOLCHAINS_NAME_64)-ar

#####################################################################
#        DIRECTORIES
#####################################################################

SERVICEMANAGERL_SOURCE_DIR := $(CURDIR)
SERVICEMANAGER_INC_DIR := $(CURDIR)/../base/include

BASE_INC_DIR := $(DRM_DIR)/base/include
DRMSERVICE_INC_DIR     := $(DRMSERVICE_SOURCE_DIR)/include
DRMIPC_CLIENT_INC_DIR  := $(DRMSERVICE_SOURCE_DIR)/drm_ipc/client
DRMIPC_SERVER_INC_DIR  := $(DRMSERVICE_SOURCE_DIR)/drm_ipc/server
DRMPLUGIN_INC_DIR      := $(DRMSERVICE_SOURCE_DIR)/drmplugin
DRMPLUGIN_PRIV_INC_DIR := $(DRMSERVICE_SOURCE_DIR)/drmplugin/private


LIBPATH := -L $(HI_SHARED_LIB_DIR)
#LIBPATH += -L $(HI_STATIC_LIB_DIR)

LIBPATH_64 := -L $(HI_SHARED_LIB64_DIR)
#LIBPATH_64 += -L $(HI_STATIC_LIB64_DIR)

#### open debug
###############################include *h##########################################

CFG_INC := -I$(SERVICEMANAGER_INC_DIR)
CFG_INC += -I$(SERVICEMANAGER_SOURCE_DIR)
CFG_INC += -I$(SERVICEMANAGER_INC_DIR)/arch/linux-arm

#### objs
###################################################################################
VPATH := $(SERVICEMANAGER_SRC_DIR)

EXTRA_CFLAGS :=
EXTRA_CFLAGS += -DCHIP_TYPE_$(CFG_HI_CHIP_TYPE) -DBOARD_TYPE_$(CFG_BOARD_TYPE) -DSDK_VERSION=$(CFG_SDK_VERSION)
EXTRA_CFLAGS += $(C_DEFINES)
EXTRA_CFLAGS += -c -O2 -Wall -std=c++11 $(CFG_INC) -fpermissive -fPIC
EXTRA_CFLAGS += -DBINDER_IPC_32BIT=1
EXTRA_CFLAGS += -D_GNU_SOURCE -D_XOPEN_SOURCE=600 
EXTRA_CFLAGS += -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64


LIB := -lutils -lcutils -llog -lbinder_ipc
LIB += -lpthread

BIN_NAME := service_manager
BIN_NAME_64 := service_manager_64

SERVICEMANAGER_CPP=$(notdir $(shell find $(VPATH) -maxdepth 1 -name "*.cpp"))
SERVICEMANAGER_C=$(notdir $(shell find $(VPATH) -maxdepth 1 -name "*.c"))

SERVICEMANAGER_OBJ_DIR := $(HI_OUT_DIR)/obj/$(subst $(SDK_DIR)/,,$(SERVICEMANAGERL_SOURCE_DIR))
SERVICEMANAGER_OBJ64_DIR := $(HI_OUT_DIR)/obj64/$(subst $(SDK_DIR)/,,$(SERVICEMANAGERL_SOURCE_DIR))



SERVICEMANAGER_BIN_DIR := $(SERVICEMANAGER_OBJ_DIR)/bin
SERVICEMANAGER_OUTPUT_BIN := $(SERVICEMANAGER_BIN_DIR)/$(BIN_NAME)

SERVICEMANAGER_ROOTFS_DIR := $(HI_ROOTFS_DIR)/usr/bin
SERVICEMANAGER_OUTPUT_ROOTFS_BIN := $(SERVICEMANAGER_ROOTFS_DIR)/$(BIN_NAME)


SERVICEMANAGER_BIN_DIR_64 := $(SERVICEMANAGER_OBJ64_DIR)/bin
SERVICEMANAGER_OUTPUT_BIN_64 := $(SERVICEMANAGER_BIN_DIR_64)/$(BIN_NAME_64)

SERVICEMANAGER_ROOTFS_DIR_64 :=$(HI_ROOTFS_DIR)/usr/bin
SERVICEMANAGER_OUTPUT_ROOTFS_BIN_64 := $(SERVICEMANAGER_ROOTFS_DIR_64)/$(BIN_NAME_64)



SERVICEMANAGER_OBJ := $(SERVICEMANAGER_CPP:%.cpp=$(SERVICEMANAGER_OBJ_DIR)/%.o)
SERVICEMANAGER_OBJ += $(SERVICEMANAGER_C:%.c=$(SERVICEMANAGER_OBJ_DIR)/%.o)

SERVICEMANAGER_OBJ64 := $(SERVICEMANAGER_CPP:%.cpp=$(SERVICEMANAGER_OBJ64_DIR)/%.o)
SERVICEMANAGER_OBJ64 += $(SERVICEMANAGER_C:%.c=$(SERVICEMANAGER_OBJ64_DIR)/%.o)


TARGETS :=

ifeq ($(HI_USER_SPACE_LIB),y)
TARGETS += service_manager
endif

ifeq ($(HI_USER_SPACE_LIB64),y)
TARGETS += service_manager64
endif

.PHONY: all service_manager service_manager64 service_manager_clean

all: $(TARGETS)

clean: service_manager_clean

#********************* Create out dir ***********************
service_manager: $(SERVICEMANAGER_OBJ)
	$(AT)[ -e $(SERVICEMANAGER_BIN_DIR) ] || mkdir -p $(SERVICEMANAGER_BIN_DIR)
	$(AT)$(DRM_CC) -o $(SERVICEMANAGER_OUTPUT_BIN) $(SERVICEMANAGER_OBJ)  $(LIBPATH) $(LIB)

	$(AT)[ -e $(SERVICEMANAGER_ROOTFS_DIR) ] || mkdir -p $(SERVICEMANAGER_ROOTFS_DIR)
	$(AT)cp -f $(SERVICEMANAGER_OUTPUT_BIN) $(SERVICEMANAGER_ROOTFS_DIR)

service_manager64: $(SERVICEMANAGER_OBJ64)
	$(AT)[ -e $(SERVICEMANAGER_BIN_DIR_64) ] || mkdir -p $(SERVICEMANAGER_BIN_DIR_64)
	$(AT)$(DRM_CC_64) -o $(SERVICEMANAGER_OUTPUT_BIN_64) $(SERVICEMANAGER_OBJ64)  $(LIBPATH_64) $(LIB)

	$(AT)[ -e $(SERVICEMANAGER_ROOTFS_DIR_64) ] || mkdir -p $(SERVICEMANAGER_ROOTFS_DIR_64)
	$(AT)cp -f $(SERVICEMANAGER_OUTPUT_BIN_64) $(SERVICEMANAGER_ROOTFS_DIR_64)

service_manager_clean:
	$(AT)rm -rf $(SERVICEMANAGER_OBJ_DIR) $(SERVICEMANAGER_OBJ64_DIR)
	$(AT)rm -rf $(SERVICEMANAGER_OUTPUT_BIN) $(SERVICEMANAGER_OUTPUT_BIN_64)
	$(AT)rm -rf $(SERVICEMANAGER_BIN_DIR) $(SERVICEMANAGER_BIN_DIR_64)
	$(AT)rm -rf $(SERVICEMANAGER_OUTPUT_ROOTFS_BIN) $(SERVICEMANAGER_OUTPUT_ROOTFS_BIN_64)

$(SERVICEMANAGER_OBJ_DIR)/%.o : %.c*
	$(AT)[ -e $(SERVICEMANAGER_OBJ_DIR) ] || mkdir -p $(SERVICEMANAGER_OBJ_DIR)
	$(AT)$(CFG_HI_ARM_TOOLCHAINS_NAME)-g++ $(EXTRA_CFLAGS) -o "$(SERVICEMANAGER_OBJ_DIR)/$(*F).o" "$<"

$(SERVICEMANAGER_OBJ64_DIR)/%.o : %.c*
	$(AT)[ -e $(SERVICEMANAGER_OBJ64_DIR) ] || mkdir -p $(SERVICEMANAGER_OBJ64_DIR)
	$(AT)$(CFG_HI_AARCH64_TOOLCHAINS_NAME)-g++ $(EXTRA_CFLAGS) -o "$(SERVICEMANAGER_OBJ64_DIR)/$(*F).o" "$<"